<?php
/**
 * 用户操作类
 * @author 肖小平
 *
 */
class UserAction extends Action{
	/**
	 * 个人中心
	 */
	public function user_center(){
		$where['id'] = $_SESSION['user_id'];
		$data['user_id'] = $_SESSION['user_id'];
		$data['state'] =1;
		$w['user_id']=$_SESSION['user_id'];
		$w['state']=0;
		$obj = M('User')->where($where)->find();
		$s_count = M('Contri')->where($data)->count();//查询我的投稿数
		$count = M('Contri')->where($w)->count();//查询我的草稿箱数
		$e_count= M('Enroll')->where('user_id='.$_SESSION['user_id'].' and state=1')->count();
		$this->assign('obj',$obj);
		$this->assign('s_count',$s_count);
		$this->assign('count',$count);
		$this->assign('e_count',$e_count);
		$this->display();
	}
	/**
	 * 我要投稿
	 */
	public function user_contribute(){
		if(!$_SESSION['login']){
			header('Location:'.U('Login/login'));
			exit;
		}
		$this->display();
	}
	/**
	 * 投稿详情
	 */
	public function user_contr_detail(){
		$obj = M('Article')->find($_GET['id']);
		$data['visits'] =$obj['visits']+1;
		M('Article')->where('id = '.$_GET['id'])->save($data);
		$list = M('Comment')->where('a_id='.$_GET['id'])->select();
		$this->assign('list',$list);
		$this->assign('obj',$obj);
		$this->display();
	}
	/**
	 * 我的投稿列表
	 */
	public function send_contri_list(){
		$data['user_id'] = $_SESSION['user_id'];
		$data['state'] =1;
		$list = M('Contri')->where($data)->select();
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 我的草稿箱列表
	 */
	public function draft_contri_list(){
		$data['user_id'] = $_SESSION['user_id'];
		$data['state'] =0;
		$list = M('Contri')->where($data)->select();
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 点赞
	 */
	public function user_upvote(){
		$re = M('Article')->save($_POST);
		if($re){
			echo 1;
		}else{
			echo 0;
		}
	}
	/**
	 * 发表评论
	 */
	public function user_comment(){
			$data['time'] = time();
			$data['content'] =$_POST['content'];
			$data['a_id'] =$_POST['a_id'];
			$data['username'] = $_SESSION['username'];
			$re = M('Comment')->data($data)->filter('strip_tags')->add();
			if($re){
				echo 1;
			}else{
				echo 0;
			}
	}
	/**
	 * 我的报名
	 */
	public function user_sign_up(){
		$obj = M('Enroll')->where('user_id='.$_SESSION['user_id'])->select();
		$this->assign('obj',$obj);
		$this->display();
	}
	/**
	 * 我的准考证
	 */
	public function user_stub(){
		$obj = M('Enroll')->where('user_id='.$_SESSION['user_id'].' and state=1')->select();
		$this->assign('obj',$obj);
		$this->display();
	}
	/**
	 * 查看报名电子图片
	 */
	public function img_out(){
		$obj=M('Enroll')->find($_GET['id']);
		$img = file_get_contents(C('BM_IMG_PATH').'/'.$obj['enroll_pic'],true);
		ob_end_clean();
		//使用图片头输出浏览器
		header("Content-Type: image/jpeg;");
		echo $img;
		exit;
	}
	/**
	 * 投稿操作方法
	 */
	public function send_contri(){
		$data['state'] = 1;
		$data['time'] = time();
		$data['user_id'] = $_SESSION['user_id'];
		$data['abstract'] = $_POST['abstract'];
		$data['title'] = $_POST['title'];
		$data['pic'] = $_POST['pic'];
		$data['content'] = $_POST['content'];
		$data['editor'] = $_POST['editor'];
		$data['ac_id'] = $_POST['ac_id'];
		$data['role_id'] = $_POST['role_id'];
		$re = M('Contri')->add($data);
		if($re){
			echo 1;
		}else{
			echo 0;
		}
	}
	/**
	 * 保存草稿箱
	 */
	public function draft_contri(){
		$data['state'] = 0;
		$data['time'] = time();
		$data['user_id'] = $_SESSION['user_id'];
		$data['abstract'] = $_POST['abstract'];
		$data['title'] = $_POST['title'];
		$data['pic'] = $_POST['pic'];
		$data['content'] = $_POST['content'];
		$data['editor'] = $_POST['editor'];
		$data['ac_id'] = $_POST['ac_id'];
		$data['role_id'] = $_POST['role_id'];
		$re = M('Contri')->add($_POST);
		if($re){
			echo 1;
		}else{
			echo 0;
		}
	}
	/**
	 * 删除投稿
	 */
	public function contri_delete(){
		$list = explode(',', $_POST['id']);
		foreach($list as $k=>$v){
			$re = M('Contri')->where('id='.$v)->delete();
		}
		if($re){
			echo 1;
		}else{
			echo 0;
		}
	}
	/**
	* 删除投稿
	*/
	public function draft_delete(){
		$list = explode(',', $_POST['id']);
		foreach($list as $k=>$v){
			$re = M('Contri')->where('id='.$v)->delete();
		}
		if($re){
			echo 1;
		}else{
			echo 0;
		}
	}
	/**
	 * 修改用户资料
	 */
	public function user_update(){
		if($_POST){
			$where['id'] = $_SESSION['user_id'];
			if($_POST['pic_url']){
				$data['pic_url'] = $_POST['pic_url'];
				$re = M('User')->where($where)->save($data);
			}
			if($_POST['nickname']){
				$data['nickname'] = $_POST['nickname'];
				$re = M('User')->where($where)->save($data);
			}
			if($_POST['mottos']){
				$data['mottos'] = $_POST['mottos'];
				$re = M('User')->where($where)->save($data);
			}
			$re1 = M('User')->find($_SESSION['user_id']);
			if($re){
				session('u_nickname',$re1['nickname']);
				echo 1;
			}else{
				echo 0;
			}
			exit;
		}
		$this->display();
	}
}

?>